<!DOCTYPE html>
<html class="ui-page-login">
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title></title>
		<link href="../css/mui.min.css" rel="stylesheet" />
		<link href="../css/style.css" rel="stylesheet" />
	</head>
	<body>
		<header class="mui-bar mui-bar-nav">
			<h1 class="mui-title">称量清场</h1>
		</header>
		<div class="mui-content">
			<div id='login-form' class="mui-input-group">
				<div class="mui-input-row row">
					<input id='BarCode' type="text" class="mui-input-clear BarCode" autofocus="true" placeholder="请扫描称量容器上标签二维码">
				</div>
			</div>
			<div class="mui-input-group item">
				<div class="mui-input-row">
					<label class="title">计划单号:</label>
					<label id="lblPlanNumber" class="intocontent"></label>
					<input type="hidden" id="txtPlanSnNum" name="txtPlanSnNum" value="">
				</div>
			</div>
			<div class="mui-input-group item" style="display:none;">
				<div class="mui-input-row">
					<label class="title">工单号:</label>
					<label id="lblOrderNum" class="intocontent"></label>
				</div>
			</div>
			<div class="mui-input-group item">
				<div class="mui-input-row">
					<label class="title">产品名称:</label>
					<label id="lblProductName" class="intocontent" style="font-size: 12px;"></label>
				</div>
			</div>
			<div class="mui-input-group item">
				<div class="mui-input-row">
					<label class="title">产品代号:</label>
					<label id="lblProductBarCode" class="intocontent"></label>
				</div>
			</div>
			<div class="mui-input-group item">
				<div class="mui-input-row">
					<label class="title">预计产量:</label>
					<label id="lblQty" class="intocontent"></label>
				</div>
			</div>
			<div class="mui-input-group item">
				<div class="mui-input-row">
					<label class="title">任务数:</label>
					<label id="lblTotalTask" class="intocontent"></label>
				</div>
			</div>
			<div class="mui-input-group item" style="display: none;">
				<div class="mui-input-row">
					<label class="title">已清点数:</label>
					<label id="lblScanTask" class="intocontent">0</label>
				</div>
			</div>
			<div class="mui-input-group item" style="height: 240px;">
				<div class="mui-input-row" style="height: 240px;">
					<div class="mui-scroll-wrapper">
						<div class="mui-scroll">
							<ul class="mui-table-view" id="tabList">
					    
							</ul>
						</div>
					</div>
				</div>
			</div>
			
			<div class="mui-content-padded" style="text-align: center; margin-top: 4px;">
				<button id='btnBack' type="button" class="mui-btn mui-btn-primary mui-btn-outlined">返回主页</button>
				<!-- <button id='btnDetail' type="button" class="mui-btn mui-btn-success mui-btn-outlined">查看详细</button> -->
			</div>
		</div>
		<script src="../js/mui.min.js"></script>
		<script type="text/javascript" src="../js/jquery-1.11.1.js"></script>
		<script type="text/javascript" src="../js/moment.min.js"></script>
		<script type="text/javascript" src="../js/git.mes.common.js"></script>
		<script type="text/javascript" src="../js/git.mes.key.js"></script>
		<script type="text/javascript">
			mui.plusReady(function(){
				plus.webview.currentWebview().addEventListener("show",function(e){
	                setTimeout(function() {
                        openSoftKeyboard();
                        document.getElementById("BarCode").focus();
                        closeKeyBoard();
                    	plus.key.hideSoftKeybord();
                    }, 300);
	            });
	            mui('.mui-scroll-wrapper').scroll({
					deceleration: 0.0005,
					scrollY: true, //是否竖向滚动
 					scrollX: false, //是否横向滚动
 					indicators: true, //是否显示滚动条
				});
	            
	            var PlanEntity = plus.webview.currentWebview();
				var listTask=undefined;

				var SetTable=function(){
					var WaitHtml="";
					var CompHtml="";
					var WaterHtml="";
					if(listTask!=undefined && listTask.length>0){
						for(var i=0;i<listTask.length;i++){
							var item=listTask[i];
							var SendTime=git.JsonToDateTime(item.SendTime);

							var html="";
							html+='<li class="mui-table-view-cell mui-media">';
								html+='<a href="javascript:;">';
									if(!git.IsEmpty(SendTime)){
										html+='<span class="mui-badge mui-badge-success">已清</span>';
									}else{
										html+='<span class="mui-badge mui-badge-purple">待</span>';
									}
									html+='<div class="mui-media-body">';
										html+="原料代号:&nbsp;"+item.PartBarCode;
										var DemQty=item.DemQty*1000;
										var RealQty=item.RealQty*1000;
										html+='<p class="mui-ellipsis">实际/要求(g):&nbsp;'+git.ToDecimal(RealQty,4)+'/'+git.ToDecimal(DemQty,4)+'</p>';
										
										if(parseFloat(RealQty)>0){
											html+='<p class="mui-ellipsis">批次:&nbsp;'+item.BatchNum+'</p>';
										}else{
											html+='<p class="mui-ellipsis"><span class="mui-badge  mui-badge-warning">未称量</span></p>';
										}
										
									html+='</div>';
								html+='</a>';
							html+='</li>';

							if(item.PartBarCode=="D00"){
								WaterHtml+=html;
							}else{
								if(!git.IsEmpty(SendTime)){
									CompHtml+=html;
								}else{
									WaitHtml+=html;
								}
							}
						}
					}
					document.getElementById('tabList').innerHTML = WaitHtml+CompHtml+WaterHtml;
				}
				
	            var GetTaskList=function(BarCode){
					var param={};
					param["PlanNumber"]=BarCode;
					param["PageIndex"]=1;
					param["PageSize"]=1000;
					git.gitAjax({
						url:WeightApiName.WeightApiName_GetPage,
						data:param,
						type:"get",
						success:function(result){
							if(result.Code==1 && result.Result!=undefined){
								listTask=result.Result;

								var ScanCount=0;
								for(var i=0;i<listTask.length;i++){
									var SendTime=listTask[i].SendTime;
									SendTime=git.JsonToDateTime(SendTime);
									if(!git.IsEmpty(SendTime)){
										ScanCount++;
									}
								}
								$("#lblTotalTask").html(ScanCount+"&nbsp;/&nbsp;"+listTask.length);
								$("#lblScanTask").text(ScanCount);
								SetTable();
							}else{
								git.PlayerError();
								mui.toast(result.Message,{ duration:'long', type:'div' }); 
							}
						}
					});
				}

				if(PlanEntity!=undefined){
					$("#lblPlanNumber").html(PlanEntity.PlanNumber+"&nbsp;&nbsp;&nbsp;/&nbsp;&nbsp;&nbsp;"+PlanEntity.OrderNum);
					$("#lblOrderNum").text(PlanEntity.OrderNum);
					$("#txtPlanSnNum").val(PlanEntity.SnNum);
					$("#lblCusName").text(PlanEntity.CusName);
					$("#lblProductName").text(PlanEntity.ProductName);
					$("#lblProductBarCode").html(PlanEntity.BarCode+"&nbsp;&nbsp;&nbsp;/&nbsp;&nbsp;&nbsp;"+PlanEntity.BomNumber);
					$("#lblBomNumber").text(PlanEntity.BomNumber);
					$("#lblQty").text(PlanEntity.PlanQty+"kg");
					GetTaskList(PlanEntity.PlanNumber);
				}
				
				
				var CheckScan=function(BarCode){
					if(listTask!=undefined && listTask.length>0){
						var Flag=false;
						for(var i=0;i<listTask.length;i++){
							if(listTask[i].SnNum==BarCode){
								Flag=true;
								break;
							}
						}
						if(!Flag){
							git.PlayerError();
							mui.toast('该物料不在此生产工单范围之内',{ duration:'long', type:'div' }); 
							return false;
						}
					}
					var LoginUser=git.GetObjectCache(CacheKey.LoginUser);
					var UserNum=LoginUser==undefined? "":LoginUser.UserNum;
					var param={};
					param["SnNum"]=BarCode;
					param["UserNum"]=UserNum;
					param["PlanNum"]=PlanEntity.SnNum;
					git.gitAjax({
						url:WeightApiName.WeightApiName_Send,
						data:param,
						type:"get",
						success:function(result){
							if(result.Code==1){
								mui.toast(result.Message,{ duration:'long', type:'div' }); 
								GetTaskList(PlanEntity.PlanNumber);
							}else{
								git.PlayerError();
								mui.toast(result.Message,{ duration:'long', type:'div' }); 
							}
						}
					});
				}
				
				document.getElementById("BarCode").addEventListener("keypress", function(event) {
			        if (event.keyCode == "13"){
			        	var BarCode=$("#BarCode").val();
			            if(!git.IsEmpty(BarCode)){
			            	CheckScan(BarCode);
			            	$("#BarCode").val("");
			            }else{
			            	git.PlayerError();
			            }
			        }
			    });
			    
				/**
				 * 返回到功能选项主页
				 * @param  {[type]} ){					mui.openWindow({						id:"../main.html",						url:"../main.html",						createNew:false,						show:{							autoShow:true,						}					});				} [description]
				 * @return {[type]}                                                                                                                                                           [description]
				 */
				document.getElementById("btnBack").addEventListener("tap",function(){
					mui.openWindow({
						id:"../main.html",
						url:"../main.html",
						createNew:false,
						show:{
							autoShow:true,
						}
					});
				});

				/**
				 * 查看工单明细项
				 * [description]
				 * @param  {[type]} ){					mui.openWindow({						id:"../main.html",						url:"../main.html",						createNew:false,						show:{							autoShow:true,						}					});				} [description]
				 * @return {[type]}                                                                                                                                                           [description]
				 */
				// document.getElementById("btnDetail").addEventListener("tap",function(){
				// 	mui.openWindow({
				// 		id:"Detail.html",
				// 		url:"Detail.html",
				// 		createNew:true,
				// 		show:{
				// 			autoShow:true,
				// 		},
				// 		extras:{Result:listTask}
				// 	});
				// });
			});
		</script>
	</body>

</html>